using MediatR;
using Medical.Domain;
using Medical.Infrastructrue.Interfaces;
using Medical.ReadApi.Dto;
using Medical.WriteApi.Appcontext.Command;
using Medical.WriteApi.Appcontext.Command.OutpatientOrderTemplates;
using Medical.WriteApi.Dto;

namespace Medical.WriteApi.Appcontext.CommandHandler
{
	public class UpdateOutpatientOrderTemplateCommandHandler : IRequestHandler<UpdateOutpatientOrderTemplateCommand, OutpatientOrderTemplateDto>
	{
		private readonly IBaseRepository<OutpatientOrderTemplate> _repository;

		public UpdateOutpatientOrderTemplateCommandHandler(IBaseRepository<OutpatientOrderTemplate> repository)
		{
			_repository = repository;
		}

		public async Task<OutpatientOrderTemplateDto> Handle(UpdateOutpatientOrderTemplateCommand request, CancellationToken cancellationToken)
		{
			var entity = await _repository.GetModel(x => x.TemplateId == request.TemplateId && !x.IsDeleted);
			if (entity == null)
			{
				throw new Exception("模板不存在");
			}

			if (!string.IsNullOrEmpty(request.TemplateName)) entity.TemplateName = request.TemplateName;
			if (request.Description != null) entity.Description = request.Description;
			if (request.IsActive.HasValue) entity.IsActive = request.IsActive.Value;
			if (request.IsDefault.HasValue) entity.IsDefault = request.IsDefault.Value;

			if (request.ShowSerialNumber.HasValue) entity.ShowSerialNumber = request.ShowSerialNumber.Value;
			if (request.ShowDepartment.HasValue) entity.ShowDepartment = request.ShowDepartment.Value;
			if (request.ShowDoctor.HasValue) entity.ShowDoctor = request.ShowDoctor.Value;
			if (request.ShowPrescription.HasValue) entity.ShowPrescription = request.ShowPrescription.Value;
			if (request.ShowBedNumber.HasValue) entity.ShowBedNumber = request.ShowBedNumber.Value;
			if (request.ShowConsultationTime.HasValue) entity.ShowConsultationTime = request.ShowConsultationTime.Value;
			if (request.ShowAdmissionTime.HasValue) entity.ShowAdmissionTime = request.ShowAdmissionTime.Value;
			if (request.ShowDischargeTime.HasValue) entity.ShowDischargeTime = request.ShowDischargeTime.Value;
			if (request.ShowExpenseDetails.HasValue) entity.ShowExpenseDetails = request.ShowExpenseDetails.Value;
			if (request.ShowCashier.HasValue) entity.ShowCashier = request.ShowCashier.Value;
			if (request.ShowAmount.HasValue) entity.ShowAmount = request.ShowAmount.Value;

			entity.UpdatedAt = DateTime.Now;
			await _repository.UpdateAsync(entity);

			return new OutpatientOrderTemplateDto
			{
				TemplateId = entity.TemplateId,
				TemplateName = entity.TemplateName,
				Description = entity.Description,
				IsActive = entity.IsActive,
				IsDefault = entity.IsDefault,
				ShowSerialNumber = entity.ShowSerialNumber,
				ShowDepartment = entity.ShowDepartment,
				ShowDoctor = entity.ShowDoctor,
				ShowPrescription = entity.ShowPrescription,
				ShowBedNumber = entity.ShowBedNumber,
				ShowConsultationTime = entity.ShowConsultationTime,
				ShowAdmissionTime = entity.ShowAdmissionTime,
				ShowDischargeTime = entity.ShowDischargeTime,
				ShowExpenseDetails = entity.ShowExpenseDetails,
				ShowCashier = entity.ShowCashier,
				ShowAmount = entity.ShowAmount,
				CreatedAt = entity.CreatedAt,
				UpdatedAt = entity.UpdatedAt
			};
		}
	}
}
